/*
 *  This file is part of Social Media Monitoring Toolbox.
 *
 *  Social Media Monitoring Toolbox is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Social Media Monitoring Toolbox is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Social Media Monitoring Toolbox. If not, see <http://www.gnu.org/licenses/lgpl.html>.
 *
 * � Copyright 2013
 * Jana Asmussen, Julian Bau�, Daniela Dalock, Christian Gutjahr,
 * Fabian Heidorn, Alexander Kaack, Vitali Kagadij 
 * 
 */

package gui;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import javax.faces.bean.ManagedBean;

import types.AnalyseTyp;
import types.FilterTyp;
import types.GeoTag;
import types.StreamDescriptor;
import types.TweetPost;
import types.YoutubeVideoType;

import logic.ILogic;
import logic.Logic;

@ManagedBean(name = "getData")
public class GetData implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 2230828326509137403L;
	private ILogic logic = new Logic();

	/**
	 * Returns all the StreamDescriptors from the logic layer of the application
	 * 
	 * @return all the StreamDescriptors from the Logic
	 */
	public List<StreamDescriptor> getStreams() {
		return logic.getAllStreamDescriptor();
	}

	/**
	 * Returns all Twitter StreamDescriptors from the logic layer of the
	 * application
	 * 
	 * @return all the StreamDescriptors from the Logic
	 */
	public List<StreamDescriptor> getTwitterStreams() {
		return logic.getAllTwitterStreamDescriptor();
	}

	/**
	 * Returns all Youtube StreamDescriptors from the logic layer of the
	 * application
	 * 
	 * @return all the StreamDescriptors from the Logic
	 */
	public List<StreamDescriptor> getYoutubeStreams() {
		return logic.getAllYoutubeStreamDescriptor();
	}

	/**
	 * Returns the posts from the stream. If needed, the posts can be filtered.
	 * 
	 * @param streamDes
	 *            StreamDescriptor, the posts from wich will be returned
	 * @param filter
	 *            FilterTyp, that filters the posts needed to be returned
	 * @return the posts from the stream
	 */
	List<TweetPost> getTweets(StreamDescriptor streamDes, FilterTyp filter) {
		return logic.getTweetPosts(streamDes, filter);
	}

	/**
	 * Returns the posts from the stream. If needed, the posts can be filtered.
	 * 
	 * @param streamDes
	 *            StreamDescriptor, the posts from wich will be returned
	 * @param filter
	 *            FilterTyp, that filters the posts needed to be returned
	 * @return the posts from the stream
	 */
	List<YoutubeVideoType> getYouTubeClips(StreamDescriptor streamDes,
			FilterTyp filter) {
		return logic.getYouTubeVideos(streamDes, filter);
	}

	/**
	 * Returns the array, that contains positive, negative and neutral
	 * assessment of the posts in the stream from parameter in percents
	 * 
	 * @param sd
	 *            StreamDescriptor, that is used to get the analysis from
	 * @return the array of doubles with posts assessment
	 */
	double[] getPercentageSentiment(StreamDescriptor sd) {
		return logic.getPercentageSentiment(sd, null);
	}

	/**
	 * Updates the posts from Tweeter, if it is needed
	 * 
	 * @param analisys
	 *            AnalyseTyp, that shows, if update is needed
	 */
	void updateTweetPosts(AnalyseTyp analysis) {
		logic.updateTweetPosts(analysis);
	}

	/**
	 * Get the list of all geoTags
	 * 
	 * @param sd
	 *            StreamDescriptor, that is used to get the analysis from
	 * @return a list of geoTags
	 */
	HashMap<GeoTag, String> getAllGeoTags(StreamDescriptor sd) {
		return logic.getAllGeoTags(sd);
	}

	/**
	 * Get the list of all geoTags for a special sentiment
	 * 
	 * @param sd
	 *            StreamDescriptor, that is used to get the analysis from
	 * @param sentiment
	 *            1 for positive and -1 for negative
	 * @return a list of geoTags
	 */
	HashMap<GeoTag, String> getGeoTagsWithSentiment(StreamDescriptor sd,
			int sentiment) {
		return logic.getGeoTagsWithSentiment(sd, sentiment);
	}

	/**
	 * Get the StreamDescriptorfor an String id
	 * 
	 * @param sd
	 *            StreamDescriptor, that is used to get the analysis from
	 * @param sentiment
	 *            1 for positive and -1 for negative
	 * @return the StreamDescriptor or null
	 */
	StreamDescriptor getStreamDescriptor(String id,
			List<StreamDescriptor> streams) {
		StreamDescriptor returnValue = null;
		for (StreamDescriptor streamDescriptor : streams) {
			if (streamDescriptor.getStreamName().compareTo(id) == 0
					&& returnValue == null)
				returnValue = streamDescriptor;
		}
		return returnValue;
	}

	/**
	 * Get the top youtube clips - sorted
	 * 
	 * @return list of youtubeclips
	 */
	public List<YoutubeVideoType> getTopYoutubeStreams() {
		try {
			return logic.getTopYoutubePosts();
		} catch (Exception e) {
			System.out.println("Exception in logic thrown!!!");
			e.printStackTrace();
			return new ArrayList<YoutubeVideoType>();
		}
	}

	/**
	 * Returns an array filled with doubles, each describing the average
	 * sentiment for an period. Each period has a field in the array, beginning
	 * at 0.
	 * 
	 * @param streamDes
	 *            StreamDescriptor to describe the analyzed posts
	 * @param startDate
	 *            Date to be started with
	 * @param endDate
	 *            Date to declare the end of the timeframe
	 * @param interval
	 *            Integer to describe the length of one period in days
	 * @return Array filled with doubles
	 */
	public TreeMap<Date, Double> getAverageSentimentForPeriod(
			StreamDescriptor streamDes, Date startDate, Date endDate,
			int interval) {
		return logic.getAverageSentimentForPeriod(streamDes, startDate,
				endDate, interval);
	}
}